package com.shortvedio.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shortvedio.base.mapper.SUserInfoMapper;
import com.shortvedio.base.mapper.SUsersLoginMapper;
import com.shortvedio.base.mapper.UserLikeMapper;
import com.shortvedio.base.mapper.UserRelationshipsMapper;
import com.shortvedio.base.model.dto.RelationInfo;
import com.shortvedio.search.model.dto.UserAllInfoDto;
import com.shortvedio.search.service.UserInfoService;
import com.shortvedio.base.model.Result;
import com.shortvedio.base.model.po.SUserInfo;
import com.shortvedio.base.model.po.SUsersLogin;
import com.shortvedio.base.model.po.UserLike;
import com.shortvedio.base.model.po.UserRelationships;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    SUserInfoMapper sUserInfoMapper;

    @Autowired
    UserRelationshipsMapper userRelationshipsMapper;

    @Autowired
    SUsersLoginMapper sUsersLoginMapper;

    @Autowired
    UserLikeMapper userLikeMapper;


    @Override
    public Result getRelationList(Long userId) {//获取用户
        List<RelationInfo> relationInfoList = userRelationshipsMapper.getRelationInfoList(userId);
        return Result.success(relationInfoList);
    }

    private Integer getRelationCount(Long userId) {
        LambdaQueryWrapper<UserRelationships> userRelationshipsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRelationshipsLambdaQueryWrapper.eq(UserRelationships::getUserId,userId);
        Integer count = userRelationshipsMapper.selectCount(userRelationshipsLambdaQueryWrapper);
        return count;
    }

    private Integer getFansCount(Long userId) {
        LambdaQueryWrapper<UserRelationships> userRelationshipsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRelationshipsLambdaQueryWrapper.eq(UserRelationships::getTargetUserId,userId);
        Integer count = userRelationshipsMapper.selectCount(userRelationshipsLambdaQueryWrapper);
        return count;
    }

    @Override
    public Result getFansList(Long userId) {
        List<RelationInfo> fansInfoList = userRelationshipsMapper.getFansInfoList(userId);
        return Result.success(fansInfoList);
    }

    @Override
    public Result getFriendList(Long userId) {
        List<RelationInfo> friendList = userRelationshipsMapper.getFriendList(userId);
        return Result.success(friendList);
    }

    private Integer getFriendCount(Long userId) {
        LambdaQueryWrapper<UserRelationships> userRelationshipsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRelationshipsLambdaQueryWrapper.eq(UserRelationships::getUserId,userId).eq(UserRelationships::getTargetUserId,userId);
        Integer count = userRelationshipsMapper.selectCount(userRelationshipsLambdaQueryWrapper);
        return count;
    }

    private Integer getLikeCount(Long userId){
        LambdaQueryWrapper<UserLike> userLikeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLikeLambdaQueryWrapper.eq(UserLike::getGetId,userId);
        Integer count = userLikeMapper.selectCount(userLikeLambdaQueryWrapper);
        return count;
    }


    private UserAllInfoDto userAll (Long userId){
        LambdaQueryWrapper<SUserInfo> sUserInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sUserInfoLambdaQueryWrapper.eq(SUserInfo::getUserId,userId);
        SUserInfo sUserInfo = sUserInfoMapper.selectOne(sUserInfoLambdaQueryWrapper);
        if (sUserInfo==null){
            return null;
        }
        //获取账户表中的数据
        SUsersLogin sUsersLogin = sUsersLoginMapper.selectById(userId);
        if (sUsersLogin==null){
            return null;
        }
        //合并两个数据
        UserAllInfoDto userAllInfoDto = new UserAllInfoDto();
        BeanUtils.copyProperties(sUserInfo,userAllInfoDto);
        BeanUtils.copyProperties(sUsersLogin,userAllInfoDto);
        return userAllInfoDto;
    }

    public Result getUserAllInfo(Long userId) {//
        //获取用户信息表中的数据
        UserAllInfoDto userAllInfoDto = userAll(userId);
        if (userAllInfoDto==null){
            return Result.error("用户数据异常");
        }
        //获取content表中与用户有关的信息
        //获取粉丝数
        Integer fansCount = getFansCount(userId);
        userAllInfoDto.setFansCount(fansCount);
        //获取点赞数
        Integer likeCount = getLikeCount(userId);
        userAllInfoDto.setLikeCount(likeCount);
        //获取关注数
        Integer relationCount = getRelationCount(userId);
        userAllInfoDto.setAttentioncount(relationCount);
        return Result.success(userAllInfoDto);
    }

    @Override
    public Result getSelfAllInfo(Long userId) {
        //获取用户信息表中的数据
        UserAllInfoDto userAllInfoDto = userAll(userId);
        if (userAllInfoDto==null){
            return Result.error("用户数据异常");
        }
        //获取content表中与用户有关的信息
        //获取粉丝数
        Integer fansCount = getFansCount(userId);
        userAllInfoDto.setFansCount(fansCount);
        //获取朋友的数量
        Integer friendCount = getFriendCount(userId);
        userAllInfoDto.setFriendCount(friendCount);
        //获取点赞数
        Integer likeCount = getLikeCount(userId);
        userAllInfoDto.setLikeCount(likeCount);
        //获取关注数
        Integer relationCount = getRelationCount(userId);
        userAllInfoDto.setAttentioncount(relationCount);
        return Result.success(userAllInfoDto);
    }

    @Override
    public Result searchUserInfo(String keyWord) {
        LambdaQueryWrapper<SUserInfo> sUserInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sUserInfoLambdaQueryWrapper.like(SUserInfo::getName,keyWord);
        List<SUserInfo> sUserInfos = sUserInfoMapper.selectList(sUserInfoLambdaQueryWrapper);
        return Result.success(sUserInfos);
    }
}
